package com.example.demo.job;

import com.example.demo.service.PersonService;
import lombok.extern.slf4j.Slf4j;
import org.apache.shardingsphere.elasticjob.api.ShardingContext;
import org.apache.shardingsphere.elasticjob.simple.job.SimpleJob;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

/**
 * @author huchen
 * @description
 * @since 2021/11/26 17:44
 */
@Component
@Slf4j
public class PersonJob2 implements SimpleJob {

    @Autowired
    private PersonService personService;

    @Value("${server.port}")
    private Integer port;

    @Override
    public void execute(ShardingContext shardingContext) {
        String taskId = shardingContext.getTaskId();
        String shardingParameter = shardingContext.getShardingParameter();
        log.info("总分片数{}，当前分片数：{}，传入的参数：{}",shardingContext.getShardingTotalCount(),shardingContext.getShardingItem()
                ,shardingContext.getShardingParameter());
        log.info("端口：{},定时任务开始：taskId:{},shardingParameter:{}",port,taskId,shardingParameter);
        personService.doSomething(Integer.valueOf(shardingParameter), shardingContext.getShardingItem());
    }
}
